/*
 * Copyright (c) jiucheng.org
 */
package org.jiucheng.template.function;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @author jiucheng
 *
 */
public class Sys {
    
    //日期格式化对象缓存
    private static Map<String, SimpleDateFormat> sdfs = new HashMap<String, SimpleDateFormat>();
    
    public static String ver() {
        return "by jiucheng \n";
    }
    
    public static String helloworld() {
        return "hello world";
    }
    
    public static String format(Date date, String format) {
        if (date == null) {
            return null;
        }
        if(format == null) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = sdfs.get(format);
        if(sdf != null) {
            return sdf.format(date);
        }
        sdf = new SimpleDateFormat(format);
        sdfs.put(format, sdf);
        return sdf.format(date);
    }
    
    public static Date datetime() {
        return Calendar.getInstance().getTime();
    }
    
    public static Object defaultValue(Object obj, Object defaultValue) {
        return ifnull(obj, defaultValue);
    }
    
    public static Object ifnull(Object obj, Object defVal) {
        if(obj != null) {
            return obj;
        }
        return defVal;
    }
}
